//
//  BaseViewController.swift
//  BaseProj
//
//  Created by 陈宇 on 2017/4/26.
//  Copyright © 2017年 科匠武汉. All rights reserved.
//

#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif
import IQKeyboardManagerSwift

import UIKit

class BaseViewController: UIViewController {
    
    lazy var disposeBag = DisposeBag()
    
    var returnKeyHandler = IQKeyboardReturnKeyHandler()
    
    // MARK: - Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureLeftItem()

        configureUI()
        
        configureEvent()

        setLocalizeText()
        
        let _ = NotificationCenter.default.rx.notification(NSNotification.Name(LCLLanguageChangeNotification), object: nil).distinctUntilChanged().takeUntil(self.rx.deallocated).subscribeOn(MainScheduler.instance).subscribe(onNext: { [weak self] (noti) in
            self?.setLocalizeText()
        })
        
        returnKeyHandler.addResponderFromView(view)
        returnKeyHandler.lastTextFieldReturnKeyType = .done
    }
    
    override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }
    
    func configureLeftItem() {
        guard let nav = self.navigationController else {
            return
        }
        
        if nav.childViewControllers.count > 1 {
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "common.back"), style: .plain, target: nil, action: nil)
            let _ = self.navigationItem.leftBarButtonItem?.rx.tap.throttle(0.1, scheduler: MainScheduler.instance).takeUntil(self.rx.deallocated).subscribe(onNext: {
                self.navigationController?.popViewController(animated: true)
            })
        }
    }

    /// 配置相关UI属性
    func configureUI() {
        view.backgroundColor = APP.VIEW_BG_COLOR
        automaticallyAdjustsScrollViewInsets = false
    }
    
    /// 配置相关事件
    func configureEvent() {
        
    }

    /// APP内部更改APP语言，会调用的方法。APP国际化支持。
    func setLocalizeText() {
        
    }
    
    // MARK: - Project needs
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

}
